package com.quan.weixin.rpc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quan.weixin.WeixinConstant;
import com.quan.weixin.model.Article;
import com.quan.weixin.model.BaseMsg;
import com.quan.weixin.model.NewsMessage;
import com.quan.weixin.model.TextMessage;
import com.quan.weixin.rpc.api.WeixinArticleGroupService;
import com.quan.weixin.rpc.api.WeixinArticleService;
import com.quan.weixin.rpc.api.WeixinKeywordService;
import com.quan.weixin.rpc.api.WeixinMenuService;
import com.quan.weixin.rpc.api.WeixinMsgService;
import com.quan.weixin.rpc.api.WeixinSubscribeService;
import com.quan.weixin.rpc.api.WeixinTextService;
import com.quan.weixin.rpc.model.WeixinArticle;
import com.quan.weixin.rpc.model.WeixinKeyword;
import com.quan.weixin.rpc.model.WeixinMenu;
import com.quan.weixin.rpc.model.WeixinSubscribe;
import com.quan.weixin.rpc.model.WeixinText;

@Service
public class WeixinMsgServiceImpl implements WeixinMsgService {
	
	@Autowired
	private WeixinMenuService menuService ;
	
	@Autowired
	private WeixinSubscribeService subscribeService ;
	
	@Autowired
	private WeixinKeywordService keywordService ;
	
	@Autowired
	private WeixinTextService textService ;
	
	@Autowired
	private WeixinArticleService articleService ;
	
	@Autowired
	private WeixinArticleGroupService articleGroupService ;

	@Override
	public Object subscribeHandler(BaseMsg baseMsg) {
		WeixinSubscribe subscribe = this.subscribeService.loadSubscribe() ;
		
		if(subscribe.getStatus() == 1) {
			if(StringUtils.isNotBlank(subscribe.getTemplateType())) {
				//回复文本内容
				return getData(baseMsg, subscribe.getTemplateType(), subscribe.getTemplateId()) ;
			} else {
				//回复文字内容
				return getDataText(baseMsg, subscribe.getReply()) ;
			}
		}
		return null;
	}
	
	@Override
	public Object clickEventHandler(BaseMsg baseMsg) {
		String eventKey = baseMsg.getEventKey().trim();

		WeixinMenu menu = this.menuService.selectMenyByKey(eventKey);
		if (null != menu) {
			return getData(baseMsg, menu.getTemplateType(), menu.getTemplateId());
		}
		return null;
	}

	@Override
	public Object textKeyWordHandler(BaseMsg baseMsg) {
		String keyword = baseMsg.getContent().trim();

		WeixinKeyword weixinKeyword = this.keywordService.selectByKeyword(keyword);
		if (null != weixinKeyword && weixinKeyword.getStatus() == 1) {
			if(StringUtils.isNotBlank(weixinKeyword.getTemplateType())) {
				return getData(baseMsg, weixinKeyword.getTemplateType(), weixinKeyword.getTemplateId());
			} else {
				if(StringUtils.isNotBlank(weixinKeyword.getReply())) {
					return getDataText(baseMsg, weixinKeyword.getReply()) ;
				}
			}
		}
		return null;
	}
	
	@Override
	public Object textMsgHandler(BaseMsg baseMsg, String content) {
		return getDataText(baseMsg, content);
	}
	
	/**
	 * 回复文字内容
	 * @param baseMsg
	 * @param content
	 * @return
	 */
	public Object getDataText(BaseMsg baseMsg, String content) {
		TextMessage textMessage = new TextMessage();
		textMessage.setToUserName(baseMsg.getFromUserName());
		textMessage.setFromUserName(baseMsg.getToUserName());
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_TEXT);
		textMessage.setContent(content);
		return textMessage;
	}
	
	/**
	 * 回复单图文
	 * @param baseMsg
	 * @param article
	 * @return
	 */
	public Object getDataArticle(BaseMsg baseMsg, Article article) {
		NewsMessage newsMessage = new NewsMessage();

		newsMessage.setToUserName(baseMsg.getFromUserName());
		newsMessage.setFromUserName(baseMsg.getToUserName());
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_NEWS);
		newsMessage.setArticleCount(1);

		List<Article> articles = new ArrayList<Article>();
		articles.add(article);
		newsMessage.setArticles(articles);
		return newsMessage;
	}
	
	/**
	 * 回复（文本，单图文，多图文）内容
	 * @param baseMsg
	 * @param templateType
	 * @param templateId
	 * @return
	 */
	public Object getData(BaseMsg baseMsg, String templateType, Integer templateId) {
		if (WeixinConstant.REPLY_MSG_TYPE_TEXT.equals(templateType)) {
			WeixinText text = this.textService.selectById(templateId);

			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(baseMsg.getFromUserName());
			textMessage.setFromUserName(baseMsg.getToUserName());
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setContent(text.getContent());

			return textMessage;
		} else if (WeixinConstant.REPLY_MSG_TYPE_ARTICLE.equals(templateType)) {
			WeixinArticle wxArticle = this.articleService.selectById(templateId);
			NewsMessage newsMessage = new NewsMessage();

			newsMessage.setToUserName(baseMsg.getFromUserName());
			newsMessage.setFromUserName(baseMsg.getToUserName());
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_NEWS);
			newsMessage.setArticleCount(1);

			List<Article> articles = new ArrayList<Article>();

			Article item = new Article();
			item.setTitle(wxArticle.getTitle());
			item.setPicUrl(wxArticle.getPicUrl());
			item.setUrl(wxArticle.getUrl());
			item.setDescription(wxArticle.getDescription());

			articles.add(item);
			newsMessage.setArticles(articles);

			return newsMessage;
		} else if (WeixinConstant.REPLY_MSG_TYPE_ARTICLES.equals(templateType)) {
			List<WeixinArticle> list = this.articleGroupService.getArticles(templateId);

			NewsMessage newsMessage = new NewsMessage();

			newsMessage.setToUserName(baseMsg.getFromUserName());
			newsMessage.setFromUserName(baseMsg.getToUserName());
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(WeixinConstant.RESP_MESSAGE_TYPE_NEWS);
			newsMessage.setArticleCount(list.size());

			List<Article> articles = new ArrayList<Article>();

			for (WeixinArticle article : list) {
				Article item = new Article();
				item.setTitle(article.getTitle());
				item.setPicUrl(article.getPicUrl());
				item.setUrl(article.getUrl());
				item.setDescription(article.getDescription());

				articles.add(item);
			}

			newsMessage.setArticles(articles);

			return newsMessage;
		}
		return null;
	}

}
